package com.dcm.item.controller;


import com.dcm.common.annotation.EnableResponseBodyAdvice;
import com.dcm.common.exception.MyException;
import com.dcm.item.entity.CategoryEntity;
import com.dcm.item.service.CategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:zhouyuhan
 * 时间 2021/3/22 11:12
 */
@Api(tags = "CategoryController")
@RestController
@RequestMapping("/category")
@EnableResponseBodyAdvice
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public List<CategoryEntity> findAll(){
        return categoryService.findAll();
    }

    /**
     * 添加
     * @param tbCategory
     * @return
     */
    @ApiOperation("添加")
    @PostMapping("/add")
    public void add(@RequestBody CategoryEntity tbCategory) throws MyException {

         categoryService.add(tbCategory);

    }

    /**
     * 根据id修改状态
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("根据id修改状态 0显示 1不显示")
    @PutMapping("/updateStatus")
    public void updateStatus(@RequestParam(value = "id",defaultValue ="" ,required = false)Long id,
                             @RequestParam(value = "status",defaultValue = "" ,required = false)Integer status) throws MyException {
        categoryService.updateStatus(id,status);

    }


    /**
     * 根据id修改整条数据
     * @param tbCategory
     * @return
     */
    @ApiOperation("根据id修改整条数据")
    @PutMapping("/update")
    public void update(@RequestBody CategoryEntity tbCategory) throws MyException {
          categoryService.update(tbCategory);

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation("根据id删除整条数据")
    @DeleteMapping("/delete")
    public void delete(@RequestParam(value = "id",required = false)Long id) throws MyException {
          categoryService.delete(id);
    }

    /**
     * 根据状态 姓名 模糊查询
     * @param status
     * @param name
     * @return
     */
    @ApiOperation("状态姓名查询")
    @GetMapping("/findByStatusAndName")
    public List<CategoryEntity> findByStatusAndName(@RequestParam(value = "status",defaultValue = "",required = false)Integer status,
                                                    @RequestParam(value = "name",defaultValue = "",required = false)String name)throws MyException {
        return categoryService.findByStatusAndName(status,name);
    }

    /**
     * 添加子类
     * @param category
     * @throws MyException
     */
    @ApiOperation("添加下级分类")
    @PostMapping("/addSecondCategory")
    public void addSecondCategory(@RequestBody CategoryEntity category) throws MyException {
        categoryService.addSecondCategory(category);
    }

    @ApiOperation("分页")
    @GetMapping("findPage")
    public PageInfo<CategoryEntity> findPage(@RequestParam(value = "pageSize",required = false) Integer pageSize,
                                             @RequestParam(value = "currentPage",required = false) Integer currentPage) {
        return categoryService.findPage(currentPage,pageSize);
    }

}
